<template>
  <div class="text-left padding-lr">
    <div class="flex align-center justify-between">
      <el-button-group>
        <el-button type="primary" size="mini" @click="selectGroup('空运')"
          >空运</el-button
        >
        <el-button
          type="primary"
          size="mini"
          @click="selectGroup('海运拼箱重量')"
          >海运拼箱重量</el-button
        >
        <el-button
          type="primary"
          size="mini"
          @click="selectGroup('海运拼箱体积')"
          >海运拼箱体积</el-button
        >
        <el-button
          type="primary"
          size="mini"
          @click="selectGroup('海运整柜')"
        >海运整柜</el-button
        >
        <el-button type="primary" size="mini" @click="selectGroup('快递')"
          >快递</el-button
        >
        <el-button type="primary" size="mini" @click="selectAll"
          >全选</el-button
        >
        <el-button type="primary" size="mini" @click="noSelectAll"
          >全不选</el-button
        >
      </el-button-group>

      <el-button type="success" size="mini" @click="saveInfo">保存</el-button>
    </div>
    <el-form>
      <el-form-item label="客户渠道">
        <el-select
          style="width:90%"
          size="mini"
          v-model="selectValue"
          multiple
          placeholder="请选择"
        >
          <el-option-group
            v-for="group in optionsData"
            :key="group.label"
            :label="group.label"
          >
            <el-option
              v-for="item in group.options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-option-group>
        </el-select>
      </el-form-item>
      <el-form-item label="统一调价">
        <el-input-number
          :controls="false"
          size="mini"
          v-model="price"
        ></el-input-number>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { ajax } from "@/store/yg/common/utils";
export default {
  data() {
    return {
      optionsData: [],
      selectValue: [],
      price: 0
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    async init() {
      const { data: manage } = await ajax("customerGroupUniformPriceMangae", {
        customerGroupId: this.$store.state.edit.id
      });
      if (!manage) return;

      console.log("this.priceInfo :>> ", manage);
      console.log("Express :>> ", manage.Express);
      this.selectValue = Object.values(manage.checkCustomerChannelIds);

      let Express = {
        label: "快递",
        options: []
      };
      let LCLVolume = {
        label: "海运拼箱体积",
        options: []
      };
      let LCLWeight = {
        label: "海运拼箱重量",
        options: []
      };
      let ZX = {
        label: "空运",
        options: []
      };
      let FCL = {
        label: "海运整柜",
        options: []
      };

      for (const k in manage.Express) {
        Express.options.push({
          label: manage.Express[k],
          value: parseInt(k)
        });
      }
      for (const k in manage.LCLVolume) {
        LCLVolume.options.push({
          label: manage.LCLVolume[k],
          value: parseInt(k)
        });
      }
      for (const k in manage.LCLWeight) {
        LCLWeight.options.push({
          label: manage.LCLWeight[k],
          value: parseInt(k)
        });
      }
      for (const k in manage.ZX) {
        ZX.options.push({
          label: manage.ZX[k],
          value: parseInt(k)
        });
      }
      for (const k in manage.FCL) {
        FCL.options.push({
          label: manage.FCL[k],
          value: parseInt(k)
        });
      }

      this.optionsData = [ZX, LCLWeight, LCLVolume, Express, FCL];
    },
    selectGroup(name) {
      const a = this.optionsData.find(x => x.label === name);
      let arr = [];
      for (const i of a.options) {
        arr.push(i.value);
      }
      this.selectValue = arr;
    },
    selectAll() {
      let arr = [];
      for (const i of this.optionsData) {
        for (const j of i.options) {
          arr.push(j.value);
        }
      }
      this.selectValue = arr;
    },
    noSelectAll() {
      this.selectValue = [];
    },

    async saveInfo() {
      console.log("selectValue :>> ", this.selectValue);
      const data = await ajax("batchSaveOrUpdateCustomerGroupChannel", {
        customerGroupId: this.$store.state.edit.id,
        customerChannelIds: this.selectValue,
        price: this.price
      });

      if (data.code === 200) {
        this.$message({
          type: "success",
          message: "保存成功"
        });
      } else {
        this.$message.error(data.message);
      }
    }
  }
};
</script>

<style></style>
